<?php $this->_extends('_layouts/basic_layout'); ?>

<?php $this->_block('resource'); ?>

<?php $this->_endblock(); ?>

<?php $this->_block('title'); ?>
	Html Templates
<?php $this->_endblock(); ?>

<?php $this->_block('admin_contents'); ?>

<div class="row-fluid">	
	<a class="btn btn-info" href="<?php echo url('htmltemplates/searchnew');?>">搜索磁盘上的新模板</a>
</div>

<div class="row-fluid">		<!-- sortable ui-sortable -->
	<div class="box span12">
		<div data-original-title="" class="box-header well">
			<h2><i class="icon-user"></i> 已添加的Html模板</h2>
		</div>
		<div class="box-content">
			<div role="grid">
				<table class="table table-striped table-bordered">		<!--  bootstrap-datatable datatable dataTable -->
					<thead>
						<tr role="row">
							<th style="width: 5%;">类别</th>	<!-- class="sorting_asc" -->
							
							<th style="width: 5%;">名称</th>	<!--  class="sorting" -->
							<th style="width: 10%;">描述</th>
							<th style="width: 5%;">根目录文件夹</th>
							<th style="width: 8%;">缩略图 - 小</th>
							<th style="width: 8%;">缩略图 - 大</th>
							<th style="width: 5%;">预览首页</th>
							<th style="width: 5%;">下载包</th>
							<th style="width: 5%;">下载/点击</th>
							<th style="width: 9%;">生成日期</th>
							<th style="width: 10%;">操作</th>
						</tr>
					</thead>   
			  
					<tbody>
						<?php if(!empty($htmlTemplatesAdded)): foreach($htmlTemplatesAdded as $ht): ?>
							<tr>
								<td class="center types">
									<span class="label type_area"><?php echo $ht['type_area']; ?></span>
									<span class="label type_color"><?php echo $ht['type_color']; ?></span>
									<span class="label type_columnwidth"><?php echo $ht['type_columnwidth']; ?></span>
									<span class="label type_sidebarcorner"><?php echo $ht['type_sidebarcorner']; ?></span>
									<span class="label type_using"><?php echo $ht['type_using']; ?></span>
								</td>
								
								<td class="name"><?php echo $ht['name']; ?></td>
								<td class="description"><?php echo $ht['description']; ?></td>
								<td class="root_foldername"><?php echo $ht['root_foldername']; ?></td>
								<td class="thumb_small">
									<img alt="<?php echo $ht['name']; ?>" width="100" height="70" src="<?php echo $htmltemp_url . $ht['root_foldername'] . '/' . $ht['thumb_small']; ?>" />
									<?php echo $ht['thumb_small']; ?>
								</td>
								<td class="thumb_big">
									<img alt="<?php echo $ht['name']; ?>" width="100" height="70" src="<?php echo $htmltemp_url . $ht['root_foldername'] . '/' . $ht['thumb_big']; ?>" />
									<?php echo $ht['thumb_big']; ?>
								</td>
								<td class="preview_indexpage"><?php echo $ht['preview_indexpage']; ?></td>
								<td class="download_pack"><?php echo $ht['download_pack']; ?></td>
								<td>
									<?php echo $ht['download_num']; ?> / <?php echo $ht['view_num']; ?>
								</td>
								<td><?php echo $ht['created_date']; ?></td>
								<td>
									<a href="javascript: void(0);" class="btn btn-info edit_btn" id="<?php echo $ht['id']; ?>">
										<i class="icon-edit icon-white"></i>  
										编辑                                        
									</a>
									<a href="<?php echo url('htmltemplates/index', array('ope' => 'delete', 'id' => $ht['id'])); ?>" class="btn btn-danger">
										<i class="icon-trash icon-white"></i> 
										删除
									</a>
								</td>
							</tr>
						<?php endforeach; endif; ?>
					</tbody>
				</table>
			</div>
		</div>
	</div>
</div>

<form id="edit_block" class="modal hide fade in form-horizontal">
	<input type="hidden" id="edit_id" name="form[id]" />
	<div class="modal-header">
		<button data-dismiss="modal" class="close" type="button">×</button>
		<h3 id="edit_foldername">编辑</h3>
	</div>
	<div class="modal-body">
		<fieldset>
			<div class="control-group">
				<label for="edit_name" class="control-label">模板名称:</label>
				<div class="controls">
					<input class="input-xlarge focused" id="edit_name" name="form[name]" type="text" />
				</div>
			</div>
			
			<div class="control-group">
				<label for="edit_desc" class="control-label">描述:</label>
				<div class="controls">
					<textarea id="edit_desc" name="form[description]"></textarea>
				</div>
			</div>
			
			<div class="control-group">
				<label for="edit_rootfolder" class="control-label">根目录文件夹:</label>
				<div class="controls">
					<input class="input-xlarge focused" id="edit_rootfolder" class="input-xlarge disabled" type="text" disabled="disabled" />
				</div>
			</div>
			<div class="control-group">
				<label for="edit_thum_s" class="control-label">预览图（小）:</label>
				<div class="controls">
					<select id="edit_thum_s" name="form[thumb_small]"></select>
				</div>
			</div>
			<div class="control-group">
				<label for="edit_thum_b" class="control-label">预览图（大）:</label>
				<div class="controls">
					<select id="edit_thum_b" name="form[thumb_big]"></select>
				</div>
			</div>
			<div class="control-group">
				<label for="edit_previewindex" class="control-label">预览主页:</label>
				<div class="controls">
					<select id="edit_previewindex" name="form[preview_indexpage]"></select>
				</div>
			</div>
			<div class="control-group">
				<label for="edit_downloadpack" class="control-label">下载包:</label>
				<div class="controls">
					<select id="edit_downloadpack" name="form[download_pack]"></select>
				</div>
			</div>
			<div class="control-group">
				<label for="edit_type_area" class="control-label">区域分类:</label>
				<div class="controls">
					<select id="edit_type_area" name="form[type_area_id]">
						<?php if(!empty($htmltemplateTypes['type_area'])): foreach($htmltemplateTypes['type_area'] as $type): ?>
							<option value="<?php echo $type['id']; ?>"><?php echo $type['name']; ?>(<?php echo $type['name_en']; ?>)</option>
						<?php endforeach; endif; ?>
					</select>
				</div>
			</div>
			<div class="control-group">
				<label for="edit_type_color" class="control-label">颜色分类:</label>
				<div class="controls">
					<select id="edit_type_color" name="form[type_color_id]">
						<?php if(!empty($htmltemplateTypes['type_color'])): foreach($htmltemplateTypes['type_color'] as $type): ?>
							<option value="<?php echo $type['id']; ?>"><?php echo $type['name']; ?>(<?php echo $type['name_en']; ?>)</option>
						<?php endforeach; endif; ?>
					</select>
				</div>
			</div>
			<div class="control-group">
				<label for="edit_type_columnwidth" class="control-label">行列分类:</label>
				<div class="controls">
					<select id="edit_type_columnwidth" name="form[type_columnwidth_id]">
						<?php if(!empty($htmltemplateTypes['type_columnwidth'])): foreach($htmltemplateTypes['type_columnwidth'] as $type): ?>
							<option value="<?php echo $type['id']; ?>"><?php echo $type['name']; ?>(<?php echo $type['name_en']; ?>)</option>
						<?php endforeach; endif; ?>
					</select>
				</div>
			</div>
			<div class="control-group">
				<label for="edit_type_sidebarcorner" class="control-label">侧边栏分类:</label>
				<div class="controls">
					<select id="edit_type_sidebarcorner" name="form[type_sidebarcorner_id]">
						<?php if(!empty($htmltemplateTypes['type_sidebarcorner'])): foreach($htmltemplateTypes['type_sidebarcorner'] as $type): ?>
							<option value="<?php echo $type['id']; ?>"><?php echo $type['name']; ?>(<?php echo $type['name_en']; ?>)</option>
						<?php endforeach; endif; ?>
					</select>
				</div>
			</div>
			<div class="control-group">
				<label for="edit_type_using" class="control-label">用途分类:</label>
				<div class="controls">
					<select id="edit_type_using" name="form[type_using_id]">
						<?php if(!empty($htmltemplateTypes['type_using'])): foreach($htmltemplateTypes['type_using'] as $type): ?>
							<option value="<?php echo $type['id']; ?>"><?php echo $type['name']; ?>(<?php echo $type['name_en']; ?>)</option>
						<?php endforeach; endif; ?>
					</select>
				</div>
			</div>
		</fieldset>
	</div>
	<div class="modal-footer">
		<a data-dismiss="modal" class="btn cancel" href="#">取消</a>
		<a class="btn btn-primary save" href="#">保存</a>
	</div>
</form>
	
<script>
	$('.edit_btn').click(function(){
		$('.currEdit').removeClass('currEdit');
		$(this).parent().parent('tr').addClass('currEdit');
		
		var id = $(this).attr('id');
		$.ajax({
			url: '<?php echo url('htmltemplates/getdiskinfo'); ?>',
			type: 'get',
			data: {id: id},
			success: function(ret){
				var infoDisk = ret.infoDisk;
				var infoSaved = ret.infoSaved;
				
				var files = infoDisk.file;
	
				$('#edit_block #edit_foldername').text(infoSaved.name);
				$('#edit_block #edit_name').val(infoSaved.name);
				$('#edit_block #edit_desc').val(infoSaved.description);
				$('#edit_block #edit_rootfolder').val(infoSaved.root_foldername);
				$('#edit_block #edit_thum_s').html('');
				$('#edit_block #edit_thum_b').html('');
				$('#edit_block #edit_previewindex').html('');
				$('#edit_block #edit_downloadpack').html('');
	
				var imgExts = new Array('png', 'jpg', 'jpeg', 'bmp', 'gif');
				var indexPages = new Array('index', 'main');
				var pageExts = new Array('php', 'htm', 'html');
				var zipExts = new Array('rar', 'zip');
				$.each(files, function(k, value){
					var ext = getFileExt(value);
					if(imgExts.hasVal(ext)){
						var imgObj = new Image();
						imgObj.src = '<?php echo $htmltemp_url; ?>'+ infoSaved.root_foldername + '/' + value;
						
						$('#edit_block #edit_thum_s').append('<option value="'+value+'">('+imgObj.width+'*'+imgObj.height+')'+value+'</option>');
						$('#edit_block #edit_thum_b').append('<option value="'+value+'">('+imgObj.width+'*'+imgObj.height+')'+value+'</option>');
					}
	
					if(pageExts.hasVal(ext)){
						$('#edit_block #edit_previewindex').append('<option value="'+value+'">'+value+'</option>');
						if(indexPages.hasVal(getFileNameNoExt(value))){
							$('#edit_block #edit_previewindex').val(value);
						}
					}
	
					if(zipExts.hasVal(ext)){
						$('#edit_block #edit_downloadpack').append('<option value="'+value+'">'+value+'</option>');
					}
				});
	
				$('#edit_block #edit_id').val(id);
				$('#edit_block #edit_thum_s').val(infoSaved.thumb_small);
				$('#edit_block #edit_thum_b').val(infoSaved.thumb_big);
				$('#edit_block #edit_previewindex').val(infoSaved.preview_indexpage);
				$('#edit_block #edit_downloadpack').val(infoSaved.download_pack);
	
				$('#edit_block #edit_type_area').val(infoSaved.type_area_id);
				$('#edit_block #edit_type_color').val(infoSaved.type_color_id);
				$('#edit_block #edit_type_columnwidth').val(infoSaved.type_columnwidth_id);
				$('#edit_block #edit_type_sidebarcorner').val(infoSaved.type_sidebarcorner_id);
				$('#edit_block #edit_type_using').val(infoSaved.type_using_id);
				
				$('#editErrors').html('');
				
				$('#edit_block').modal('show');
			},
			dataType: 'json'
		});
		
	});

	$('.modal-footer .save').click(function(){
		$.ajax({
			url: '<?php echo url('htmltemplates/edit'); ?>',
			type: 'post',
			data: $('#edit_block').serialize(),
			success: function(ret){
				if(ret.result){
					$('.currEdit .types .type_color').html($('#edit_block #edit_type_color').find("option:selected").text());
					$('.currEdit .types .type_area').html($('#edit_block #edit_type_area').find("option:selected").text());
					$('.currEdit .types .type_columnwidth').html($('#edit_block #edit_type_columnwidth').find("option:selected").text());
					$('.currEdit .types .type_sidebarcorner').html($('#edit_block #edit_type_sidebarcorner').find("option:selected").text());
					$('.currEdit .types .type_using').html($('#edit_block #edit_type_using').find("option:selected").text());
					
					$('.currEdit .name').html($('#edit_block #edit_name').val());
					$('.currEdit .description').html($('#edit_block #edit_desc').val());
					$('.currEdit .root_foldername').html($('#edit_block #edit_rootfolder').val());
					$('.currEdit .preview_indexpage').html($('#edit_block #edit_previewindex').val());
					$('.currEdit .download_pack').html($('#edit_block #edit_downloadpack').val());

					$('.currEdit .thumb_small').html('<img alt="'+$('#edit_block #edit_name').val()+'" width="100" height="70" src="<?php echo $htmltemp_url; ?>'+$("#edit_block #edit_rootfolder").val()+'/'+$("#edit_block #edit_thum_s").val()+'" />'+$("#edit_block #edit_thum_s").val()+'');
					$('.currEdit .thumb_big').html('<img alt="'+$('#edit_block #edit_name').val()+'" width="100" height="70" src="<?php echo $htmltemp_url; ?>'+$("#edit_block #edit_rootfolder").val()+'/'+$("#edit_block #edit_thum_b").val()+'" />'+$("#edit_block #edit_thum_b").val()+'');
					
					$('#edit_block').modal('hide');
				}else{
					$('#editErrors').html('');
					$.each(ret.errorinfo, function(item, errors){
						var erritems = $('<li />');
						erritems.append(item);
						var ul = $('<ul />');
						$.each(errors, function(k, error){
							ul.append('<li class="error">'+error+'</li>');
						});
						erritems.append(ul);
						$('#editErrors').append(erritems);
					});
				}
			},
			dataType: 'json'
		});
	});
	$('.modal-footer .cancel').click(function(){
		$('.currEdit').removeClass('currEdit');
	});
</script>

<?php $this->_endblock(); ?>